package cn.edu.zucc.JDBCUtil;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class DBConnection {
	
	private static final String FILEPATH = "configServer/jdbc.properties";
	
	private static String jdbcUrl;
	private static String dbUser, dbPwd;

	public static Connection getConnection(){
		try {
			return  DriverManager.getConnection(jdbcUrl, dbUser,	dbPwd);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static  void releaseConnection(Connection conn){
		try {
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public static void init(){
		FileInputStream input=null;
		try {
			input = new FileInputStream(FILEPATH);
		} catch (FileNotFoundException ex) {
			ex.printStackTrace();
		}
		Properties p = new Properties();
		try {
			p.load(input);
		} catch (IOException e1) {
			
		}
		String dbDriverClassName=p.getProperty("driver");
		try {
			Class.forName(dbDriverClassName);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		jdbcUrl=p.getProperty("jdbcurl");
		dbUser=p.getProperty("dbuser");
		dbPwd=p.getProperty("pwd");
	}
	static {
		init();
	}

}
